package com.login;

import java.io.IOException;

import com.login.io.FileIO;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LockScreenActivity extends Activity{

	private String masterKey=null;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //try to retrieve key
        try {
        	masterKey = FileIO.loadMasterKey(this);
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		
		//select the layout
		if(masterKey != null)
			setContentView(R.layout.password_input_layout);
		else
		{
			setContentView(R.layout.password_input_first_layout);
			Toast.makeText(LockScreenActivity.this, "Password not set, please set it now", Toast.LENGTH_LONG).show();	
		}
		
		
		if(masterKey != null)
		{
		
		final EditText pass = (EditText)findViewById(R.id.passEdit);
		
		Button enter = (Button)findViewById(R.id.enterButton);
		enter.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) 
            {
            	String currentValue = pass.getText().toString();
            	
            	if(masterKey ==null){
              		 Toast.makeText(LockScreenActivity.this, "Master NOT set", Toast.LENGTH_LONG).show();
              	}
              	
              	else if(masterKey.equals(currentValue.toString()))
              	{
              		Intent intent = new Intent(getApplicationContext(), LoginListActivity.class);
              		
              		Bundle b = new Bundle();
            		b.putString("KEY", currentValue);
            		intent.putExtras(b);
              		
              		startActivity(intent);
              		finish();
              	}
              	else
              	{
              		 Toast.makeText(LockScreenActivity.this, "Invalid Password: " + currentValue.toString(), Toast.LENGTH_LONG).show();
              	}
            		
            	
            }
        });
		
		}
		else
		{
			final EditText pass = (EditText)findViewById(R.id.passFirstEdit);
			final EditText pass2 = (EditText)findViewById(R.id.passFirstEditVerify);
			
			Button enter = (Button)findViewById(R.id.enterButtonFirst);
			enter.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) 
	            {
	            	if(pass.getText().toString().equals(pass2.getText().toString()))
	            	{
	            		
	            		try {
							FileIO.saveMasterKey(LockScreenActivity.this, pass.getText().toString());
						} catch (IOException e) {
							e.printStackTrace();
						} catch (Exception e) {
							e.printStackTrace();
						}
						
	            		Intent intent = new Intent(getApplicationContext(), LoginListActivity.class);
	            		
	            		Bundle b = new Bundle();
	            		b.putString("KEY", pass.getText().toString());
	            		intent.putExtras(b);
	            		
	            		
	              		startActivity(intent);
	              		finish();
	            	}
	            	else
	            	{
	            		 Toast.makeText(LockScreenActivity.this, "Passwords dont match please try again", Toast.LENGTH_LONG).show();
	            	}
	            		
	            	
	            }
	        });
		}
		
//		try {
//			FileIO.saveData(this, "data.bin", "This is my data\n it is fun\n it shoyuld be encrypted", "1985");
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		try {
//			Log.d("DEBUG", FileIO.loadData(this,"data.bin", "1985"));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
		
	}
	
}
